package com.example.smartwaterapi.controller;

import com.example.smartwaterapi.common.api.RestApiResponse;
import com.example.smartwaterapi.entity.UserAchievement;
import com.example.smartwaterapi.service.UserAchievementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户成就控制器
 */
@Slf4j
@Tag(name = "用户成就管理API", description = "用户成就相关接口")
@RestController
@RequestMapping("/api/user-achievement")
public class UserAchievementController {

    @Autowired
    private UserAchievementService userAchievementService;

    @Operation(summary = "获取用户成就详情", description = "根据ID获取用户成就信息")
    @GetMapping("/{id}")
    public RestApiResponse<UserAchievement> getUserAchievement(@Parameter(description = "成就ID") @PathVariable Long id) {
        log.info("获取用户成就详情, id: {}", id);
        try {
            UserAchievement achievement = userAchievementService.getById(id);
            if (achievement == null) {
                log.warn("未找到用户成就记录, id: {}", id);
                return RestApiResponse.notFound("用户成就记录不存在");
            }
            log.info("成功获取用户成就记录, id: {}", id);
            return RestApiResponse.ok(achievement);
        } catch (Exception e) {
            log.error("获取用户成就详情异常, id: {}", id, e);
            return RestApiResponse.error("获取用户成就记录失败: " + e.getMessage());
        }
    }
    
    @Operation(summary = "创建用户成就", description = "创建新的用户成就记录")
    @PostMapping
    public RestApiResponse<UserAchievement> createUserAchievement(@RequestBody UserAchievement achievement) {
        log.info("创建用户成就记录: {}", achievement);
        try {
            boolean success = userAchievementService.save(achievement);
            if (success) {
                log.info("用户成就记录创建成功, id: {}", achievement.getId());
                return RestApiResponse.created(achievement);
            } else {
                log.warn("用户成就记录创建失败");
                return RestApiResponse.error("创建用户成就记录失败");
            }
        } catch (Exception e) {
            log.error("创建用户成就记录异常", e);
            return RestApiResponse.error("创建用户成就记录异常: " + e.getMessage());
        }
    }
    
    @Operation(summary = "更新用户成就", description = "更新已有的用户成就记录")
    @PutMapping("/{id}")
    public RestApiResponse<Boolean> updateUserAchievement(
            @Parameter(description = "成就ID") @PathVariable Long id,
            @RequestBody UserAchievement achievement) {
        log.info("更新用户成就记录, id: {}, data: {}", id, achievement);
        try {
            achievement.setId(id);
            boolean success = userAchievementService.updateById(achievement);
            if (success) {
                log.info("用户成就记录更新成功, id: {}", id);
                return RestApiResponse.ok(true);
            } else {
                log.warn("用户成就记录更新失败, id: {}", id);
                return RestApiResponse.notFound("更新用户成就记录失败，记录可能不存在");
            }
        } catch (Exception e) {
            log.error("更新用户成就记录异常, id: {}", id, e);
            return RestApiResponse.error("更新用户成就记录异常: " + e.getMessage());
        }
    }
    
    @Operation(summary = "删除用户成就", description = "删除指定的用户成就记录")
    @DeleteMapping("/{id}")
    public RestApiResponse<Boolean> deleteUserAchievement(@Parameter(description = "成就ID") @PathVariable Long id) {
        log.info("删除用户成就记录, id: {}", id);
        try {
            boolean success = userAchievementService.removeById(id);
            if (success) {
                log.info("用户成就记录删除成功, id: {}", id);
                return RestApiResponse.ok(true);
            } else {
                log.warn("用户成就记录删除失败, id: {}", id);
                return RestApiResponse.notFound("删除用户成就记录失败，记录可能不存在");
            }
        } catch (Exception e) {
            log.error("删除用户成就记录异常, id: {}", id, e);
            return RestApiResponse.error("删除用户成就记录异常: " + e.getMessage());
        }
    }
} 